package crawler;

import org.apache.http.Header;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.JsonUtil;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author sunzhihao2
 * @date 10:25 2018/5/25
 */
public class CrawlerForConcept {
    private static Logger logger = LoggerFactory.getLogger(Crawler.class);
    public static String find(String id,CloseableHttpClient closeableHttpClient){

        //创建
        HttpGet httpGet = new HttpGet("https://wows-api.wallstreetcn.com/v3/aioria/stock/relateds?code="+id);

        //向对方服务器发送Post请求
        try {

            System.out.println("-------------------------------");

            httpGet.addHeader("UserAgent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
            httpGet.addHeader("Accept","application/json, text/plain, */*");
            httpGet.addHeader("Accept-Encoding","gzip, deflate, br");
            httpGet.addHeader("accept-language","zh-CN,zh;q=0.9");
            httpGet.addHeader("Connection","keep-alive");



            CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpGet);
            String result = "";

            //如果模拟请求成功
            if(httpResponse.getStatusLine().getStatusCode() == 200) {
                Header[] headers = httpResponse.getAllHeaders();
                System.out.println("-------------------------------");
                String entity = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
                System.out.println(entity);
                httpGet.abort();//释放资源
                return entity;
            }

            httpGet.abort();//释放资源
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            httpGet.abort();//释放资源
        }
        return null;
    }
}
